<template>
    <div>
        <!-- 搜索栏 -->
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="用户信息">
                    <div class="w-[250px]">
                        <el-input
                            v-model="queryParams.user"
                            placeholder="请输入用户编号/昵称/手机号"
                            clearable
                            @keyup.enter="resetPaging"
                        />
                    </div>
                </el-form-item>
                <el-form-item label="退款单号">
                    <div class="w-[250px]">
                        <el-input
                            v-model="queryParams.order_sn"
                            placeholder="请输入退款单号"
                            clearable
                            @keyup.enter="resetPaging"
                        />
                    </div>
                </el-form-item>
                <el-form-item label="来源单号">
                    <div class="w-[250px]">
                        <el-input
                            v-model="queryParams.refund_sn"
                            placeholder="请输入来源单号"
                            clearable
                            @keyup.enter="resetPaging"
                        />
                    </div>
                </el-form-item>
                <el-form-item label="来源场景">
                    <el-select
                        v-model="queryParams.scene"
                        class="w-[250px]"
                        placeholder="请选择"
                    >
                        <el-option value="" label="全部"/>
                        <el-option value="recharge" label="充值"/>
                        <el-option value="subscribe" label="订阅"/>
                    </el-select>
                </el-form-item>
                <el-form-item label="记录时间">
                    <date-picker
                        v-model:startTime="queryParams.start_time"
                        v-model:endTime="queryParams.end_time"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPaging">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <!-- 表格栏 -->
        <el-card v-loading="pager.loading" class="!border-none" shadow="never">
            <el-tabs v-model="activeTab" @tab-change="handleTabChange">
                <el-tab-pane
                    v-for="(item, index) in tabLists"
                    :label="`${item.name}(${pager.extend[item.type] ?? 0})`"
                    :name="index"
                    :key="index"
                />
            </el-tabs>

            <el-table :data="pager.lists" size="large">
                <el-table-column label="退款单号" prop="order_sn" width="200" />
                <el-table-column label="用户信息" min-width="120">
                    <template #default="{ row }">
                        <div class="flex items-center">
                            <el-avatar :size="36" :src="row.user?.avatar"/>
                            <div class="ml-2">
                                <p>{{ row?.user?.nickname }}</p>
                                <p class="text-xs text-tx-placeholder">{{ row?.user?.sn }}</p>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="来源单号" prop="order_sn" min-width="120" />
                <el-table-column label="订单金额" prop="total_amount" min-width="100" />
                <el-table-column label="退款金额" prop="refund_amount" min-width="100" />
                <el-table-column label="退款状态" prop="pay_status" min-width="100">
                    <template #default="{ row }">
                        <el-tag type="warning" v-if="row.refund_status === 1">退款中</el-tag>
                        <el-tag type="primary" v-if="row.refund_status === 2">退款成功</el-tag>
                        <el-tag type="danger" v-if="row.refund_status === 3">退款失败</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="记录时间" prop="create_time" min-width="175" />
                <el-table-column label="操作" width="100" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-if="row.refund_status === 3"
                            type="danger"
                            link
                            @click="handleRefund(row.id)"
                        >
                            重新退款
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="flex justify-end mt-4">
                <paging v-model="pager" @change="queryLists" />
            </div>
        </el-card>

    </div>
</template>

<script setup lang="ts">
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import refundApi from '@/api/finance/refund'

const activeTab = ref<number>(0)
const tabLists = ref([
    { name: '全部', type: 'all' },
    { name: '退款中', type: 'ing' },
    { name: '退款成功', type: 'ok' },
    { name: '退款失败', type: 'fail' }
])

// 查询参数
const queryParams = reactive({
    type: '',
    user: '',
    scene: '',
    order_sn: '',
    refund_sn: '',
    start_time: '',
    end_time: ''
})

// 分页查询
const { pager, queryLists, resetParams, resetPaging } = usePaging({
    fetchFun: refundApi.lists,
    params: queryParams
})

/**
 * 切换选项
 */
const handleTabChange = async (index: any) => {
    queryParams.type = tabLists.value[index].type
    await queryLists()
}

/**
 * 发起退款
 */
const handleRefund = async (record_id: number) => {
    feedback.confirm('确定要执行重新退款吗?').then(async () => {
        await refundApi.again_refund(record_id)
        await queryLists()
        feedback.msgSuccess('发起成功')
    }).catch(() => {})
}

onMounted(async () => {
    await queryLists()
})
</script>
